﻿


var actionUrl = 'ashx/bidderhandler.ashx';
var actionUrla = 'ashx/agenthandler.ashx';
var actionUrlm = 'ashx/managerhandler.ashx';
var formUrl = '/bidder/html/bidder.html';
var formUrla = '/bidder/html/agent.html';
var formUrlm = '/bidder/html/manager.html';
var formUrlz = '/bidder/html/upload.html';
var actiondll = '/tenderinfo/ashx/baghandler.ashx';
var ids = "";
var bid = "";
$(function () {
    autoResize({ dataGrid: '#mGrid', gridType: 'datagrid', callback: mygrid.bindGrid, height: 5 });
    $('#bid').combobox({
        multiple: false, valueField: 'KeyId', textField: 'bagname', panelHeight: 'auto', onLoadSuccess: function () {
            var data = $('#bid').combobox('getData');
            if (data.length > 0)
                $("#bid").combobox('select', data[0].KeyId);
        }
    });
    $.getJSON(actiondll + '?' + crud.createParam('dll'), function (d) {
        $('#bid').combobox('loadData', d);        
    });
    $('#a_add').click(crud.add);
    $('#a_edit').click(crud.edit);
    $('#a_delete').click(crud.del);
    $('#setbg').click(crud.setbag);
});

var mygrid = {
    bindGrid: function (size) {
        $('#mGrid').datagrid({
            toolbar: '#toolbar',
            title: '投标商列表',
            iconCls: 'icon icon-list',
            width: size.width,
            height: size.height,
            nowrap: false,
            rownumbers: true,
            animate: true,
            url: actionUrl,
            columns: [[
                { field: 'ckb', checkbox: true },
				{ title: '投标商', field: 'KeyId', width: 100 },
                 {
                     title: '设置代理人', field: 'a', width: 100, formatter: function (v, d, i) {                        
                         return '<a href="#" onclick="crud.adda(' + d.KeyId + ');">设置代理人</a>';
                     }
                 },
                {
                    title: '设置项目经理', field: 'm', width: 100, formatter: function (v, d, i) {
                       
                        return '<a href="#" onclick="crud.addm(' + d.KeyId + ');">设置项目经理</a>';
                        
                    }
                },
                
                { title: '投标商编号', field: 'bbh', width: 100 },
                { title: '名称', field: 'bname', width: 100 },
				{ title: '主要业绩', field: 'bperformance', width: 100 },
				{ title: '诚信记录', field: 'bsincerity', width: 100 },
				{ title: '评标参考分值', field: 'bscore', width: 100 },
				{ title: '手机', field: 'bphone', width: 100 },
				{ title: '股东名单', field: 'bshareholders', width: 100 },
				{ title: '注册资本', field: 'bcapital', width: 100 },
				{ title: '资质', field: 'bqualifications', width: 100 },
				{ title: '经营范围', field: 'bmanagement', width: 100 },
				{ title: '公司成立时间', field: 'bdatetime', width: 100 },
				{ title: '输入时间', field: 'binputtime', width: 100 },
				{ title: '地址', field: 'badd', width: 100 },
				{ title: '法人代表', field: 'blegal', width: 100 },
				{ title: '固定电话', field: 'btel', width: 100 },
				{ title: '传真', field: 'bfax', width: 100 },
				{ title: '邮箱', field: 'bemail', width: 100 },
				{ title: '户籍所在地', field: 'bhuji', width: 100 }
            ]]
        });
    },
    reload: function () {
        $('#mGrid').datagrid('clearSelections').datagrid('reload');
    },
    selectRow: function () {
        return $('#mGrid').datagrid('getSelected');
    },
    getids: function () {
        var rows = $('#mGrid').datagrid('getSelections');
        if (rows == null || rows.length == 0) {
            $.messager.alert("提示", "请选择要设置的行！", "info");
            return;
        }
        ids = "";
            for (var i = 0; i < rows.length; i++)
                ids = ids + rows[i].KeyId + ",";
            if (ids != "")
                ids = ids.substr(0, ids.length - 1);           
    }
}

var crud = {
    createParam: function (action, keyid) {
        var o = {};
        var query = top.$('#myForm').serializeArray();
        query = convertArray(query);
        o.jsonEntity = JSON.stringify(query);
        o.action = action;
        o.keyid = keyid;
        return "json=" + JSON.stringify(o);
    },
    add: function () {
        var addDialog = top.$.hDialog({
            href: formUrl + '?v=' + Math.random(), height:400,width:500, title: "增加", iconCls: 'icon-add',
            onLoad: function () {
                top.$('#userTab').tabs({
                    onSelect: function () {
                        top.$('.validatebox-tip').remove();
                    }
                });
            },
            submit: function () {
                if (top.$('#myForm').validate().form()) {
                    var query = crud.createParam('add', 0);
                    $.ajaxjson(actionUrl, query, function (d) {
                        if (d.Success) {
                            mygrid.reload();
                            addDialog.dialog('close');
                            msg.ok(d.Message);
                        } else {
                            MessageOrRedirect(d);
                        }
                    });
                }
                return false;
            }
        });
    },
    adda: function (id) {
        var addDialog = top.$.hDialog({
            href: formUrla + '?v=' + Math.random(), height: 400, width: 500, title: "增加", iconCls: 'icon-add',
            onLoad: function () {
                //top.$('#userTab').tabs({
                //    onSelect: function () {
                //        top.$('.validatebox-tip').remove();
                //    }
                //});
            },
            submit: function () {
                if (top.$('#myForm').validate().form()) {
                    var query = crud.createParam('add', 0);
                    $.ajaxjson(actionUrla, query+"&bid="+id, function (d) {
                        if (d.Success) {
                            mygrid.reload();
                            addDialog.dialog('close');
                            msg.ok(d.Message);
                        } else {
                            MessageOrRedirect(d);
                        }
                    });
                }
                return false;
            }
        });
    },
    
    addm: function (id) {
        var addDialog = top.$.hDialog({
            href: formUrlm + '?v=' + Math.random(), height: 400, width: 500, title: "增加", iconCls: 'icon-add',
            onLoad: function () {
                //top.$('#userTab').tabs({
                //    onSelect: function () {
                //        top.$('.validatebox-tip').remove();
                //    }
                //});
            },
            submit: function () {
                if (top.$('#myForm').validate().form()) {
                    var query = crud.createParam('add', 0);
                    $.ajaxjson(actionUrlm, query + "&bid=" + id, function (d) {
                        if (d.Success) {
                            mygrid.reload();
                            addDialog.dialog('close');
                            msg.ok(d.Message);
                        } else {
                            MessageOrRedirect(d);
                        }
                    });
                }
                return false;
            }
        });
    },
    edit: function () {
        var row = mygrid.selectRow();
        if (row) {
            var editDialog = top.$.hDialog({
                href: formUrl + '?v=' + Math.random(), height: 400, width: 500, title: "修改", iconCls: 'icon-edit',
                onLoad: function () {
                    top.$('#userTab').tabs({
                        onSelect: function () {
                            top.$('.validatebox-tip').remove();
                        }
                    });
                    var viewModel = top.ko.mapping.fromJS(row);
                    top.ko.applyBindings(viewModel);
                },
                submit: function () {
                    if (top.$('#myForm').validate().form()) {
                        var query = crud.createParam('edit', row.KeyId);
                        $.ajaxjson(actionUrl, query, function (d) {
                            if (d.Success) {
                                mygrid.reload();
                                editDialog.dialog('close');
                                msg.ok(d.Message);
                            } else {
                                MessageOrRedirect(d);
                            }
                        });
                    }
                    return false;
                }
            });
        } else {
            msg.warning('请选择要修改的数据');
            return false;
        }
        return false;
    },
    del: function () {
        var row = mygrid.selectRow();
        if (row) {
            if (confirm('确认要删除此数据吗？')) {
                $.ajaxjson(actionUrl, crud.createParam("del", row.KeyId), function (d) {
                    if (d.Success) {
                        mygrid.reload();
                        msg.ok(d.Message);
                    } else {
                        MessageOrRedirect(d);
                    }
                });
            }

        }
        else {
            msg.warning('请选择要删除的数据.');
        }
        return false;
    },
    setbag: function () {
        mygrid.getids();
        var bagid = $("#bid").combobox('getValue');
        if(ids!="")
        $.ajaxjson(actionUrl, "action=setbag&keyids="+ids+"&bagid="+bagid, function (d) {
            if (d>0) {
                //mygrid.reload();
                msg.ok('成功设置'+d+"项！");
            } else {
                msg.ok('失败！');
            }
        });
    }
}